<?php
/**
 * 巡展基金-项目-线下活动-线下明细
 * @author Administrator
 *
 */
class xPODetailModel extends MY_Model{
    function xPODetailModel(){
        parent::MY_Model();
        $this->table = 'XPODetail';
    }
    /**
     * 重新组装数据(non-PHPdoc)
     * @see MY_Model::getNewList()
     */
    function getNewList($where = '', $limit = NULL, $offset = NULL, $colum = '', $orderby = ''){
        $findList = parent::getNewList($where, $limit, $offset, $colum, $orderby);
        $list = array();
        foreach ($findList as $v){
            if (isset($v['costNo'])){
                $list[$v['costNo']] = $v;
            }else{
                $list[] = $v;
            }
        }
        return $list;
    }
    /**
     * 更新父级金额(non-PHPdoc)
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->CI->load->model('xPOfflineModel');
        $this->CI->xPOfflineModel->updateMoney($parentId);
    }
    /**
     * 获取父级关联ID(non-PHPdoc)
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['offlineCostNo']) && $data['offlineCostNo'] ? (int)$data['offlineCostNo'] : 0;
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        $xunDetailType = $this->config->item('xunDetailType');
        $xunDetailName = $this->config->item('xunDetailName');
        foreach ($list as $k => $v){
            $list[$k]['detailTypeStr'] = isset($xunDetailType[$v['detailType']]) ? $xunDetailType[$v['detailType']] : '';
            $list[$k]['detailNameStr'] = isset($xunDetailName[$v['detailType']][$v['detailName']]) ? $xunDetailName[$v['detailType']][$v['detailName']] : '';
        }
        return $list;
    }
    /**
     * 获取父级汇总金额
     * @param int $offlineId
     */
    function getMoney($offlineCostNo){
        $findList = $this->getList(array('offlineCostNo'=>$offlineCostNo));
        $data = array(
            'budgetApply' => 0,
            'settleApply' => 0,//结算申请金额
        );
        foreach($findList as $v){
            $data['budgetApply'] += $v['budgetApply'];
            $data['settleApply'] += $v['settleApply'];
        }
        return $data;
    }
    /**
     * 根据条件提供的线下活动ID集查询各详细数据(主要给汇总表导数据用)
     * @param array $offlineActList 带键值的线下活动列表
     */
    function getMoneyDetailList($offlineList = array()){
        $findList = $this->getList(array('offlineCostNo' => array('in',array_keys($offlineList))));
        $initData = array(
            'budgetApply' => array(
                'venue'     => 0,
                'activity'  => 0,
                'total'     => 0,
            ),
            'budgetAudit' => array(
                'venue'     => 0,
                'activity'  => 0,
                'total'     => 0,
            ),
            'settleApply' => array(
                'venue'     => 0,
                'activity'  => 0,
                'total'     => 0,
            ),
            'settleAudit' => array(
                'venue'     => 0,
                'activity'  => 0,
                'total'     => 0,
            ),
        );
        $returnList = array();
        foreach ($offlineList as $k => $v){
            $actId = $v['costNo'];
            //初始化
            isset($returnList[$actId]) || $returnList[$actId] = $initData;
        }
        foreach($findList as $v){
            $actId = $v['offlineCostNo'];
            $key = '';
            if ($v['detailType'] == 1){//场馆配套费用
                $key = 'venue';
            }else if($v['detailType'] == 2){//活动组织费用
                $key = 'activity';
            }
            //预算申请金额
            $returnList[$actId]['budgetApply'][$key] += $v['budgetApply'];
            $returnList[$actId]['budgetApply']['total'] += $v['budgetApply'];
            //预算审核金额(无预算审核，默认为预算审核金额)
            $returnList[$actId]['budgetAudit'][$key] += $v['budgetApply'];
            $returnList[$actId]['budgetAudit']['total'] += $v['budgetApply'];
            //结算申请金额
            $returnList[$actId]['settleApply'][$key] += $v['settleApply'];
            $returnList[$actId]['settleApply']['total'] += $v['settleApply'];
            //结算审核金额
            if ($offlineList[$actId]['settleAuditRs'] == 1){
                $returnList[$actId]['settleAudit'][$key] += $v['settleApply'];
                $returnList[$actId]['settleAudit']['total'] += $v['settleApply'];
            }
        }
        return $returnList;
    }
}